<?php

namespace App\Models;

use App\Http\Controllers\LibApi\CqstLibController;
use App\Http\Controllers\LibApi\GshqLibController;
use App\Http\Controllers\QrCodeController;
use App\Http\Controllers\ScoreRuleController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;


/**
 * 用户微信信息
 * Class ArticleTypeModel
 * @package app\common\model
 */
class UserLibraryInfo extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';

    public $table = 'user_account_lib';

    /**
     * 根据account_id 获取读者证号
     */
    public function getAccountByAccountId($account_id)
    {
        $account = $this->where('id', $account_id)->value('account');
        if (empty($account)) {
            return false; //未绑定
        }
        return $account;
    }

    /**
     * 绑定读者证
     * @param 
     */
    public function add($data, $field = [])
    {
        $this->account = $data['cardnumber'];
        $this->username = $data['surname'];
        $this->password = $data['password'];
        $this->id_card = $data['IDcard'];
        $this->sex = $data['sex'] == 'F' ? 2 : 1;
        $this->tel = $data['phone'];
        $this->type = $this->getCardType($data['cardType']);
        $this->time = $data['dateenrolled'];
        $this->end_time = $data['datexpiry'];
        $this->pavilion = $data['branchname'];


        //获取注册积分
        $scoreRuleObj = new ScoreRuleController();
        $score_info = $scoreRuleObj->getScoreByType(1);
        if (!empty($score_info['score'])) {
            $this->score = $score_info['score'];
        }

        $this->save();

        if (!empty($score_info['score'])) {
            //添加积分推送信息
            $scoreRuleObj->addScoreLog([
                'account_id' => $this->id,
                'score' => $score_info['score'],
                'type_id' => 1,
                'type' => $score_info['type_name'],
                'intro' => $score_info['score'] > 0 ? '绑定读者证增加 ' . $score_info['score'] . ' 积分' :  '绑定读者证减少 ' . abs($score_info['score']) . ' 积分',
            ]);
        }

        return $score_info['score'];
    }

    /**
     * 修改绑定读者证
     * @param 
     */
    public function change($data, $res = null, $findWhere = [])
    {
        if (empty($res)) {
            $res = $this;

            //生成读者证二维码
            $qrCodeObj = new QrCodeController();
            $res->qr_url = $qrCodeObj->setQr($data['cardno'], true);
        }
        $res->account = $data['cardno'];
        $res->username = $data['realName'];
        $res->password = $data['password'];
        $res->id_card = isset($data['idno']) ? $data['idno'] : null;
        $res->sex = $data['genderNote'] == '男' ? 1 : 2;
        $res->tel = !empty($data['mobile']) ? $data['mobile'] : '';
        $res->type = $data['cardTypeNote'];
        $res->time = $data['regDate']; //正式环境
        $res->end_time = $data['endDate'];
        $res->pavilion = $data['sublib']; //正式环境
        $res->pavilion_note = $data['sublibNote'];
        $res->status_card = $data['statusNote'];

        $res->save();
        return $res->qr_url;
    }



    //判断读者证号密码是否正确 和是否绑定读者证
    public function checkAccountPwdIsNormal($user_id, $account_id = null)
    {
        if (empty($account_id)) {
            //获取用户当前信息
            $user_info = UserInfo::find($user_id);

            if (empty($user_info)) {
                throw new \Exception('用户信息获取失败');
            }
            if (empty($user_info['account_id'])) {
                throw new \Exception('该用户未绑定读者证号，请先绑定读者证');
            }

            $account_id = $user_info['account_id'];
        }

        $account_lib_info = UserLibraryInfo::find($account_id);
        if (empty($account_lib_info)) {
            throw new \Exception('读者证号获取失败，请稍后重试');
        }

        //判断读者证号的密码是否过期
        $libapiObj = new CqstLibController();
        $res = $libapiObj->login($account_lib_info->account, $account_lib_info->password);
        if ($res['code'] == 200) {
            $account_lib_info['account_id'] = $account_lib_info['id']; //多返回一个参数
            return $account_lib_info; //读者证号账号密码正确
        }
        throw new \Exception('读者证密码错误');
    }

    /**
     * 判断读者证是否被其他用户绑定
     */
    public function isBindAccount($account_id, $user_id = null)
    {
        $user_info = UserInfo::where('account_id', $account_id)->first();
        if ($user_info && $user_info['id'] != $user_id) {
            // $nickname = UserWechatInfo::where('id', $user_info['wechat_id'])->value('nickname');
            return '“' . $user_info['serial_number'] . '”已绑定此读者证号，请先解绑'; //未绑定
        }
        return true;
    }
}
